package entity

import (
	"time"
	"xr-game-server/core/constants/db"
	"xr-game-server/core/math"
	"xr-game-server/core/migrate"
	"xr-game-server/core/snowflake"
	"xr-game-server/core/syndb"
)

const (
	TbPlayerTaskDetail = "player_task_details"
)

const (
	PlayerTaskDetailTaskId     db.TbCol = "task_id"
	PlayerTaskDetailVal        db.TbCol = "val"
	PlayerTaskDetailOrderIndex db.TbCol = "order_index"
)

type PlayerTaskDetail struct {
	migrate.OneModel
	TaskId     uint64 `gorm:"comment:任务id"`
	Val        uint64 `gorm:"comment:完成值"`
	OrderIndex uint8  `gorm:"default:0;comment:顺序"`
}

func initTaskDetail() {
	syndb.RegLazyWithMiddle(TbPlayerTaskDetail, db.CreatedAtName)
	syndb.RegLazyWithMiddle(TbPlayerTaskDetail, db.UpdatedAtName)

	syndb.RegLazyWithMiddle(TbPlayerTaskDetail, PlayerTaskDetailTaskId)
	syndb.RegLazyWithMiddle(TbPlayerTaskDetail, PlayerTaskDetailVal)
	syndb.RegLazyWithMiddle(TbPlayerTaskDetail, PlayerTaskDetailOrderIndex)
	migrate.AutoMigrate(&PlayerTaskDetail{})
}

func NewPlayerTaskDetail(taskId uint64) *PlayerTaskDetail {
	ret := &PlayerTaskDetail{}
	ret.ID = snowflake.GetId()
	ret.SetTaskId(taskId)
	return ret
}

func (receiver *PlayerTaskDetail) SetTaskId(taskId uint64) {
	receiver.TaskId = taskId
	syndb.AddDataToLazyChan(TbPlayerTaskDetail, PlayerTaskDetailTaskId, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: taskId,
	})
}
func (receiver *PlayerTaskDetail) SetVal(val uint64) {
	receiver.Val = val
	receiver.SetUpdatedAt(time.Now())
	syndb.AddDataToLazyChan(TbPlayerTaskDetail, PlayerTaskDetailVal, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: val,
	})
}

func (receiver *PlayerTaskDetail) AddVal(val uint64) {
	ret := math.ChkUInt64(receiver.Val, val)
	receiver.SetVal(ret + receiver.Val)
}
func (receiver *PlayerTaskDetail) SetOrderIndex(val uint8) {
	receiver.OrderIndex = val
	receiver.SetUpdatedAt(time.Now())
	syndb.AddDataToLazyChan(TbPlayerTaskDetail, PlayerTaskDetailOrderIndex, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: val,
	})
}
func (receiver *PlayerTaskDetail) SetCreatedAt(val time.Time) {
	receiver.CreatedAt = val
	syndb.AddDataToLazyChan(TbPlayerTaskDetail, db.CreatedAtName, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: val,
	})
}
func (receiver *PlayerTaskDetail) SetUpdatedAt(val time.Time) {
	receiver.UpdatedAt = val
	syndb.AddDataToLazyChan(TbPlayerTaskDetail, db.UpdatedAtName, &syndb.ColData{
		IdVal:  receiver.ID,
		ColVal: val,
	})
}
